#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
    高级特性
    @Author 黄海峰
    @Date 2021/4/14 14:02
    @Version 1.0.0
"""
arr = []
for item in range(100):
    a: str = '元素【' + str(item + 1) + '】'
    arr.append(a)
print(arr)

print('====================')
print(arr[0], arr[49], arr[99])

print('====================')
print(arr[0:4])
print(arr[2:4])
print(arr[:4])
print(arr[-5:-3])
print(arr[:-4])
print(arr[-4:-1])
print(arr[-1])
# 以下为空
print(arr[-4:0])


# 普通循环、带下标的循环
print('====================')
userList = ['a', 'b', 'c']
for user in userList:
    print('普通循环：', user)
print('====================')

for i, user in enumerate(userList):
    print('索引循环：', i, ' ', user)

print('====================')
for x, y in [(1, 1), (2, 3)]:
    print(x, '~', y)


print(list(range(5)))
print(list(range(1, 5)))


# 斐波那契数列 --除第一个和第二个数外，任意一个数都可由前两个数相加得到
def fib(max):
    n, a, b = 0, 0, 1
    while n < max:
        print(b)
        a, b = b, a + b
        # 以上语句相当于以下语句 t是元祖tuple
        # t = (b, a + b)
        # a = t[0]
        # b = t[1]
        n = n + 1
    return 'done'
print(fib(5))

# yield 可以使field变成generator的函数，在每次调用next()的时候执行，遇到yield语句返回，再次执行时从上次返回的yield语句处继续执行。
def fib2(max):
    n, a, b = 0, 0, 1
    while n < max:
        yield b
        a, b = b, a + b
        n = n + 1
    return 'done'

def odd():
    print('step 1')
    yield 1
    print('step 2')
    yield(3)
    print('step 3')
    yield(5)

print(odd())